package com.spring.sunflower.main.presenter

import com.spring.sunflower.base.BaseObserver
import com.spring.sunflower.base.BasePresenter
import com.spring.sunflower.bean.*
import com.spring.sunflower.constant.Constant
import com.spring.sunflower.main.MainContract
import io.reactivex.rxjava3.core.Observable

/**
 * Created by Kevin on 2021/8/22<br/>
 *
 * Describe:<br/>
 */
class VideoShowPresenter(baseView: MainContract.VideoShowView) :
    BasePresenter< MainContract.VideoShowView>(baseView) {

    fun getVideoList(pageIndex: String) {
        val observable =
            requestDataSource.requestVideoListRecommend(pageIndex)
        addDisposable(observable, object : BaseObserver<VideoPlayBean>() {
            override fun onSuccess(bean: VideoPlayBean?) {
              baseView.showVideoList(bean!!.datas)
            }

            override fun onError(status: Int, msg: String?) {
            }

        })

    }
    fun getVideoShowLike(likeFlg: String, videoId: String) {
        val observable =
            requestDataSource.getVideoShowLike(likeFlg, videoId)
        addDisposable(observable, object : BaseObserver<CommonBean>(baseView) {
            override fun onSuccess(bean: CommonBean?) {
                baseView.showVideoLikeSuccess()
            }

            override fun onError(status: Int, msg: String?) {
            }

        })
    }

    fun setFollowed(anchorId: String?, set: String?) {
        val observable = requestDataSource.requestFollowedSet(anchorId, set)
        addDisposable(observable, object : BaseObserver<CommonBean>(baseView) {
            override fun onSuccess(bean: CommonBean) {
                baseView.setFollowedSuccess()
            }

            override fun onError(status: Int, msg: String) {
                baseView.setFollowedFailed()
            }
        })
    }

    /**
     * @param userId   本人id
     * @param anchorId 对方id
     */
    fun applyChat(userId: String?, anchorId: String?) {
        val gender = getField(Constant.KEY_GENDER, "1")
        val observable: Observable<LiveApplyChatBean>
        observable = if ("1" == gender) {
            requestDataSource.liveApplyChat(userId, anchorId)
        } else {
            requestDataSource.liveApplyChatAnchor(anchorId)
        }
        addDisposable(observable, object : BaseObserver<LiveApplyChatBean>(baseView) {
            override fun onSuccess(bean: LiveApplyChatBean) {
                baseView.liveApplySuccess()
            }

            override fun onError(status: Int, msg: String) {}
        })
    }

    fun sayHello(anchorId: String?,userId: String?) {
        val observable = requestDataSource.greeted(anchorId,userId)
        addDisposable(observable, object : BaseObserver<BlackHasBean>(baseView) {
            override fun onSuccess(bean: BlackHasBean) {
//                baseView.hideLoading();
                baseView.sayHelloSuccess()
            }

            override fun onError(status: Int, msg: String) {
//                baseView.sayHelloFailed(msg);
            }
        })
    }

    fun releaseComment(content: String, videoId: String) {
        val observable = requestDataSource.releaseComment(content, videoId)
        addDisposable(observable, object : BaseObserver<VideoCommentReleaseBean>(baseView) {
            override fun onSuccess(bean: VideoCommentReleaseBean) {
                baseView.releaseCommentSuccess(bean.datas)
            }

            override fun onError(status: Int, msg: String) {
            }
        })
    }

    fun releaseSubComment(content: String, parentCommentId: String) {
        val observable = requestDataSource.releaseSubComment(content, parentCommentId)
        addDisposable(observable, object : BaseObserver<VideoSubCommentReleaseBean>(baseView) {
            override fun onSuccess(bean: VideoSubCommentReleaseBean) {
                baseView.releaseSubCommentSuccess(bean.datas)
            }

            override fun onError(status: Int, msg: String) {
            }
        })
    }

    fun getComment(pageIndex: String, videoId: String) {
        val observable = requestDataSource.getCommentList(pageIndex, videoId)
        addDisposable(observable, object : BaseObserver<VideoCommentBean>(baseView) {
            override fun onSuccess(bean: VideoCommentBean) {
                baseView.showComment(bean.datas)
            }

            override fun onError(status: Int, msg: String) {
            }
        })
    }

    fun getSubComment(content: String, rootCommentId: String) {
        val observable = requestDataSource.getSubCommentList(content, rootCommentId)
        addDisposable(observable, object : BaseObserver<VideoSubCommentBean>(baseView) {
            override fun onSuccess(bean: VideoSubCommentBean) {
                baseView.showSubComment(bean.datas)
            }

            override fun onError(status: Int, msg: String) {
            }
        })
    }
    fun getVideoCommentLike(likeFlg: String,commentId:String,isChild:Boolean){
        val observable =
            requestDataSource.getVideoCommentLike(likeFlg, commentId)
        addDisposable(observable, object : BaseObserver<CommonBean>(baseView) {
            override fun onSuccess(bean: CommonBean?) {
                baseView.showCommentLikeSuccess(isChild)
            }

            override fun onError(status: Int, msg: String?) {
            }

        })
    }
}